// page-wxRegister/wxRegister.js
const app = getApp();
var bmap = require('../libs/bmap-wx.js');
var wxMarkerData = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfoData: {},
    bg_oneselfIDcardImg: '',
    oneselfIDcardImg: wx.getStorageSync('oneselfIDcardImg'),
    name: '',
    markers: [],
    latitude: '',//纬度，范围为-90~90，负数表示南纬
    longitude: '',//经度，范围为-180~180，负数表示西经
    rgcData: {},
    province: '',//省
    city: '',//市
    district: '',//区、县
    street: '', //街道
    city_code: '',
  },
  chooseImage: function (e) {
    var self = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        if (e.currentTarget.id == '0') {
          self.setData({
            name: "oneselfIDcardImg",
            path: res.tempFilePaths,
            imagesType: 'appUser/' + self.data.name + '/'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('oneselfIDcardImg', imgResponseUrl)
            self.setData({
              oneselfIDcardImg: wx.getStorageSync('oneselfIDcardImg')
            })
            if (self.data.oneselfIDcardImg != '') {
              self.setData({
                bg_oneselfIDcardImg: ''
              })
            }
          };
        }
      }
    })
  },

  makertap: function (e) {
    var that = this;
    var id = e.markerId;
    that.showSearchInfo(wxMarkerData, id);
    that.changeMarkerColor(wxMarkerData, id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (app.globalData.userInfoData) {
      that.setData({
        userInfoData: app.globalData.userInfoData
      })
    }

    var oneselfIDcardImg = that.data.oneselfIDcardImg
    if (oneselfIDcardImg == null || oneselfIDcardImg == '') {
      that.setData({
        bg_oneselfIDcardImg: app.globalData.bg_oneselfIDcardImg
      })
    }
    //获取获得地理位置的权限
    wx.authorize({ scope: "scope.userLocation" });

    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: app.globalData.ak//'您的ak'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      wxMarkerData = data.wxMarkerData;
      that.setData({
        markers: wxMarkerData
      });
      that.setData({
        latitude: wxMarkerData[0].latitude
      });
      that.setData({
        longitude: wxMarkerData[0].longitude
      });
    }
    // 发起POI检索请求 
    BMap.search({
      fail: fail,
      success: success,
      // 此处需要在相应路径放置图片文件 
      iconPath: '../../img/marker_red.png',
      // 此处需要在相应路径放置图片文件 
      iconTapPath: '../../img/marker_red.png'
    });
  },
  showSearchInfo: function (data, i) {
    var that = this;
    that.setData({
      placeData: {
        title: '名称：' + data[i].title + '\n',
        address: '地址：' + data[i].address + '\n',
        telephone: '电话：' + data[i].telephone
      }
    });
  },
  changeMarkerColor: function (data, i) {
    var that = this;
    var markers = [];
    for (var j = 0; j < data.length; j++) {
      if (j == i) {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_yellow.png";
      } else {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_red.png";
      }
      markers[j](data[j]);
    }
    that.setData({
      markers: markers
    }); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})